package com.harlov.robanuka;

import javax.microedition.khronos.opengles.GL10;


import org.anddev.andengine.engine.Engine;
import org.anddev.andengine.engine.camera.Camera;
import org.anddev.andengine.engine.camera.hud.controls.AnalogOnScreenControl;
import org.anddev.andengine.engine.camera.hud.controls.BaseOnScreenControl;
import org.anddev.andengine.engine.camera.hud.controls.AnalogOnScreenControl.IAnalogOnScreenControlListener;
import org.anddev.andengine.engine.options.EngineOptions;
import org.anddev.andengine.engine.options.EngineOptions.ScreenOrientation;
import org.anddev.andengine.engine.options.resolutionpolicy.RatioResolutionPolicy;
import org.anddev.andengine.entity.primitive.Rectangle;
import org.anddev.andengine.entity.scene.Scene;
import org.anddev.andengine.entity.scene.background.SpriteBackground;
import org.anddev.andengine.entity.shape.Shape;
import org.anddev.andengine.entity.sprite.Sprite;
import org.anddev.andengine.entity.util.FPSLogger;
import org.anddev.andengine.extension.physics.box2d.PhysicsConnector;
import org.anddev.andengine.extension.physics.box2d.PhysicsFactory;
import org.anddev.andengine.extension.physics.box2d.PhysicsWorld;
import org.anddev.andengine.extension.physics.box2d.util.Vector2Pool;
import org.anddev.andengine.sensor.accelerometer.AccelerometerData;
import org.anddev.andengine.sensor.accelerometer.IAccelerometerListener;
import org.anddev.andengine.sensor.orientation.IOrientationListener;
import org.anddev.andengine.sensor.orientation.OrientationData;
import org.anddev.andengine.ui.activity.BaseGameActivity;
import org.anddev.andengine.util.Debug;
import static org.anddev.andengine.extension.physics.box2d.util.constants.PhysicsConstants.PIXEL_TO_METER_RATIO_DEFAULT;

import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.Body;
import com.badlogic.gdx.physics.box2d.FixtureDef;
import com.badlogic.gdx.physics.box2d.BodyDef.BodyType;
import com.harlov.robanuka.stages.newgame;
import com.harlov.robanuka.support.gameobjects;
import com.harlov.robanuka.support.resources;
import com.harlov.robanuka.support.gameobject.Type;
import com.harlov.robanuka.weapons.weapons_manager;

import android.hardware.SensorManager;
import android.util.DisplayMetrics;



public class RobanukaActivity extends BaseGameActivity implements IAccelerometerListener {
	
	
	
	public Camera mCamera;
	public resources mRes;
	public PhysicsWorld mPhysicsWorld;
	
	private weapons_manager WepMang;
	
	
	private gameobjects World;
	private newgame Stage_new_game;
	
	
	@Override
	public void onLoadComplete() 
	{
		
	}
	
	@Override
	public Engine onLoadEngine()
	{
		this.mCamera = new Camera(0, 0, newgame.CAMERA_WIDTH,newgame.CAMERA_HEIGHT);
		final EngineOptions options = new EngineOptions(true, ScreenOrientation.LANDSCAPE, new RatioResolutionPolicy(newgame.CAMERA_WIDTH, newgame.CAMERA_HEIGHT), this.mCamera);
		options.getTouchOptions().setRunOnUpdateThread(true);
	    return new Engine(options);
	}
	
	@Override
	public void onLoadResources()
	{
		mRes = new resources(this,getEngine());
	}
	
	@Override
	public Scene onLoadScene() {
		final Scene scene = new Scene(newgame.NUMBER_OF_LAYERS);
		mPhysicsWorld = new PhysicsWorld(new Vector2(10, 10), false);
		scene.setBackground(new SpriteBackground(new Sprite(0,0,mRes.getBack())));
		Stage_new_game = new newgame(scene,mPhysicsWorld,mRes,mEngine);
		
		
		
		World = new gameobjects(mRes,mPhysicsWorld,scene);
		World.add(Type.player,150, 150);
		
		WepMang = new weapons_manager(World);
		
		
		
		
		
		
		final AnalogOnScreenControl analogOnScreenControl = new AnalogOnScreenControl(newgame.CAMERA_WIDTH-30 - mRes.getJostfon().getWidth(), newgame.CAMERA_HEIGHT - mRes.getJostfon().getHeight(), this.mCamera, mRes.getJostfon(), mRes.getJostbut(), 0.1f, 200, new IAnalogOnScreenControlListener() {
           
			public void onControlChange(final BaseOnScreenControl pBaseOnScreenControl, final float pValueX, final float pValueY) {
				
				WepMang.setAng((int)pValueY);
				
            }

            @Override
            public void onControlClick(final AnalogOnScreenControl pAnalogOnScreenControl) {
            	
            	WepMang.shot();
                    
            }
			});
		
    analogOnScreenControl.getControlBase().setBlendFunction(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
    analogOnScreenControl.getControlBase().setAlpha(0.2f);
    analogOnScreenControl.getControlBase().setScaleCenter(0, 128);
    analogOnScreenControl.getControlBase().setScale(1.25f);
    analogOnScreenControl.getControlKnob().setScale(1.25f);
    analogOnScreenControl.refreshControlKnobPosition();

    scene.setChildScene(analogOnScreenControl); 
		
		
	    return scene;
	}
	
	@Override
	public void onAccelerometerChanged(final AccelerometerData pAccelerometerData) {
		
		final Vector2 gravity = Vector2Pool.obtain(pAccelerometerData.getX(), pAccelerometerData.getY());
		this.mPhysicsWorld.setGravity(gravity);
		Vector2Pool.recycle(gravity);		
	}  
	
	@Override
	public void onResumeGame() {
		super.onResumeGame();

		this.enableAccelerometerSensor(this);
	}
	
	@Override
	public void onPauseGame() {
		super.onPauseGame();

	this.disableAccelerometerSensor();
	}
	
	
	
	 @SuppressWarnings("unused")
	private float getScreenResolutionRatio() {
		    final DisplayMetrics dm = new DisplayMetrics();
		    getWindowManager().getDefaultDisplay().getMetrics(dm);
		    return ((float) dm.widthPixels) / ((float) dm.heightPixels);
		  }
		
}